import Layout from '@/layout'
import getPageTitle from '@/util/pageTitle'
import { getToken } from '@/util/token'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch((err) => err)
}

const routes = [
  {
    path: '/404',
    component: () => import('@/views/404'),
    meta: { title: '没有找到网页' },
    hidden: true,
  },
  {
    path: '/login',
    component: () => import('@/views/login'),
    meta: { title: 'SunMoon博客管理后台' },
    hidden: true,
  },
  {
    path: '/',
    component: Layout,
    name: 'Layout',
    redirect: '/home',
    children: [
      {
        path: 'home',
        name: 'Dashboard',
        component: () => import('@/views/dashboard'),
        meta: {
          title: '首页',
          icon: 'home',
          affix: true,
        },
      },
    ],
  },
  {
    path: '/blog',
    name: 'BlogPost',
    redirect: '/blog/post/write',
    component: Layout,
    meta: { title: '博客管理', icon: 'read' },
    children: [
      {
        path: 'post/write',
        name: 'WritePost',
        component: () => import('@/views/blog/post/Write'),
        meta: { title: '添加文章' },
      },
      {
        path: 'moment/write',
        name: 'WriteMoment',
        component: () => import('@/views/blog/moment/Write'),
        meta: { title: '添加动态' },
      },

      {
        path: 'post/edit/:id',
        name: 'EditBlog',
        component: () => import('@/views/blog/post/Write'),
        meta: { title: '编辑文章' },
        hidden: true,
      },
      {
        path: 'moment/edit/:id',
        name: 'EditMoment',
        component: () => import('@/views/blog/moment/Write'),
        meta: { title: '编辑动态' },
        hidden: true,
      },
      {
        path: 'post/list',
        name: 'PostList',
        component: () => import('@/views/blog/post/List'),
        meta: { title: '文章管理' },
      },
      {
        path: 'moment/list',
        name: 'MomentList',
        component: () => import('@/views/blog/moment/List'),
        meta: { title: '动态管理' },
      },
      {
        path: 'category/list',
        name: 'CategoryList',
        component: () => import('@/views/blog/category/List'),
        meta: { title: '分类管理' },
      },
      {
        path: 'tag/list',
        name: 'TagList',
        component: () => import('@/views/blog/tag/List'),
        meta: { title: '标签管理' },
      },
    ],
  },
  {
    path: '/website',
    name: 'Website',
    redirect: '/website/settings',
    component: Layout,
    meta: { title: '页面管理', icon: 'web' },
    children: [
      {
        path: 'settings',
        name: 'WebsiteSettings',
        component: () => import('@/views/website/Settings'),
        meta: { title: '网站设置' },
      },
      {
        path: 'person',
        name: 'Person',
        component: () => import('@/views/website/Person'),
        meta: { title: '个人资料' },
      },
    ],
  },
  {
    path: '*',
    redirect: '/404',
    hidden: true,
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
})

/**
 * resolve routes
 */

NProgress.configure({ showSpinner: false })

const whiteList = ['/login']

router.beforeEach(async (to, from, next) => {
  NProgress.start()
  // 如果有token
  const token = await getToken()

  if (token) {
    document.title = getPageTitle(to.meta.title)
    // 如果有token且是前往/login 则跳转首页
    if (to.path === '/login') {
      next({ path: '/' })
    } else {
      next()
    }
  } else {
    // 没有token
    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单，直接进入
      next()
    } else {
      next(`/login`) // 否则全部重定向到登录页
    }
  }
  NProgress.done()
})

router.afterEach(() => {
  NProgress.done()
})

export default router
